import 'package:flutter/material.dart';

// 异常分为同步异常异步异常
class Test_Exception extends StatelessWidget {
  const Test_Exception({super.key});
  onError() {
    //重置错误处理方法，自行捕获异常
    FlutterError.onError = (FlutterErrorDetails details) {
      reportError(details);
    };
  }

  reportError(FlutterErrorDetails details) {
    print('JtTag:记录报错:${details}');
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(title: const Text('Test_Exception')),
        body: Column(children: [
          ElevatedButton(
              onPressed: () {
                try {
                  Future.delayed(Duration(seconds: 1))
                      .then((e) => Future.error("xxx"));
                } catch (e) {
                  print('JtTag:记录报错:$e');
                }
              },
              child: Text('抛出异常'))
        ]));
  }
}
